package com.jtny.nytb.weather.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jtny.component.contants.GenericConstants;
import com.jtny.component.core.util.PageUtils;
import com.jtny.component.core.util.QueryWrapperUtils;
import com.jtny.component.entity.PageData;
import com.jtny.component.exception.ServiceException;
import com.jtny.component.util.BeanUtils;
import com.jtny.component.util.DateUtils;
import com.jtny.nytb.weather.dao.WeatherBasicInfoDAO;
import com.jtny.nytb.weather.dao.WeatherReturnInfoDAO;
import com.jtny.nytb.weather.entity.domain.WeatherBasicInfoDO;
import com.jtny.nytb.weather.entity.domain.WeatherReturnInfoDO;
import com.jtny.nytb.weather.entity.vo.ConfigResultsDetailVO;
import com.jtny.nytb.weather.entity.vo.WeatherBasicInfoPageQueryParamVO;
import com.jtny.nytb.weather.entity.vo.WeatherBasicInfoVO;
import com.jtny.nytb.weather.entity.vo.WeatherInfoTypeVO;
import com.jtny.nytb.weather.enums.WeatherInfoTypeEnum;
import com.jtny.nytb.weather.service.ConfigResultsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ConfigResultsServiceImpl implements ConfigResultsService {

    @Resource
    private WeatherBasicInfoDAO weatherBasicInfoDAO;

    @Resource
    private WeatherReturnInfoDAO weatherReturnInfoDAO;

    @Override
    public PageData<WeatherBasicInfoVO> pageQueryByBasicInfo(WeatherBasicInfoPageQueryParamVO weatherBasicInfoPageQueryParamVO) {
        if (weatherBasicInfoPageQueryParamVO == null) {
            weatherBasicInfoPageQueryParamVO = new WeatherBasicInfoPageQueryParamVO();
            weatherBasicInfoPageQueryParamVO.setPageNo(GenericConstants.DEFAULT_PAGE_NO);
            weatherBasicInfoPageQueryParamVO.setPageSize(GenericConstants.DEFAULT_PAGE_SIZE);
        }
        QueryWrapper<WeatherBasicInfoDO> queryWrapper = QueryWrapperUtils.getQueryWrapper(weatherBasicInfoPageQueryParamVO);
        queryWrapper.orderByDesc("id");

        Page<WeatherBasicInfoDO> page = Page.of(weatherBasicInfoPageQueryParamVO.getPageNo(), weatherBasicInfoPageQueryParamVO.getPageSize());
        PageData<WeatherBasicInfoDO> pageData = PageUtils.getPageData(weatherBasicInfoDAO.selectPage(page, queryWrapper), weatherBasicInfoPageQueryParamVO);
        if (CollUtil.isEmpty(pageData.getData())) {
            return new PageData<>();
        }
        // 时间戳转字符串
        List<WeatherBasicInfoVO> weatherBasicInfoVOList = pageData.getData().stream().map(x -> {
            WeatherBasicInfoVO weatherBasicInfoVO = BeanUtils.copyToObject(x, WeatherBasicInfoVO.class);
            weatherBasicInfoVO.setCreateTime(DateUtils.timestampToTimeStr(x.getCreateTime()));
            return weatherBasicInfoVO;
        }).collect(Collectors.toList());

        PageData<WeatherBasicInfoVO> weatherBasicInfoVOPageData = pageData.transformExcludeData(WeatherBasicInfoVO.class);
        weatherBasicInfoVOPageData.setData(weatherBasicInfoVOList);

        return weatherBasicInfoVOPageData;
    }

    @Override
    public List<Map<String, String>> selectFieldByBasicInfo(Long id) {
        if (id == null || id <= 0L) {
            throw new ServiceException("未传入基础信息的主键id");
        }
        LambdaQueryWrapper<WeatherReturnInfoDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WeatherReturnInfoDO::getBasicInfoId, id);
        List<WeatherReturnInfoDO> weatherReturnInfoDOList = weatherReturnInfoDAO.selectList(queryWrapper);
        if (CollUtil.isEmpty(weatherReturnInfoDOList)) {
            return new ArrayList<>();
        }
        // 去重
        weatherReturnInfoDOList.stream().distinct().collect(Collectors.toList());
        List<Map<String, String>> mapList = new ArrayList<>();
        // 城市表头
        Map<String, String> cityMap = new LinkedHashMap<>();
        cityMap.put("city", "城市");
        mapList.add(cityMap);
        for (WeatherReturnInfoDO weatherReturnInfoDO : weatherReturnInfoDOList) {
            Map<String, String> map = new LinkedHashMap<>();
            map.put(weatherReturnInfoDO.getParamCode(), weatherReturnInfoDO.getParamName());
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public List<WeatherBasicInfoVO> selectFieldValueByBasicInfo(Long id) {
        if (id == null || id <= 0L) {
            throw new ServiceException("未传入基础信息的主键id");
        }
        LambdaQueryWrapper<WeatherReturnInfoDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WeatherReturnInfoDO::getBasicInfoId, id);
        List<WeatherReturnInfoDO> weatherReturnInfoDOList = weatherReturnInfoDAO.selectList(queryWrapper);
        if (CollUtil.isEmpty(weatherReturnInfoDOList)) {
            return null;
        }
        // 查城市信息
        WeatherBasicInfoDO weatherBasicInfoDO = weatherBasicInfoDAO.selectById(id);

        List<ConfigResultsDetailVO> configResultsDetailVOList = BeanUtils.copyToList(weatherReturnInfoDOList, ConfigResultsDetailVO.class);
        // 按照数据产生时间倒序排序
        Map<String, List<ConfigResultsDetailVO>> resultMap = configResultsDetailVOList.stream()
                .sorted(Comparator.comparing(ConfigResultsDetailVO::getDataTime).reversed())
                .collect(Collectors.groupingBy(ConfigResultsDetailVO::getDataTime, LinkedHashMap::new, Collectors.toList()));

        // 存放返回的数据
        List<WeatherBasicInfoVO> weatherBasicInfoVOList = new ArrayList<>();
        for (Map.Entry<String, List<ConfigResultsDetailVO>> entry : resultMap.entrySet()) {
            WeatherBasicInfoVO weatherBasicInfoVO = new WeatherBasicInfoVO();
            BeanUtils.copyObject(weatherBasicInfoDO, weatherBasicInfoVO);
            weatherBasicInfoVO.setCreateTime(DateUtils.timestampToTimeStr(weatherBasicInfoDO.getCreateTime()));
            weatherBasicInfoVO.setResultsDetailList(entry.getValue());
            weatherBasicInfoVOList.add(weatherBasicInfoVO);
        }

        return weatherBasicInfoVOList;
    }

    @Override
    public List<WeatherInfoTypeVO> selectWeatherInfoType() {

        List<Integer> values = WeatherInfoTypeEnum.getValues();

        List<WeatherInfoTypeVO> weatherInfoTypeVOList = new ArrayList<>();
        for (Integer value : values) {
            WeatherInfoTypeVO weatherInfoTypeVO = new WeatherInfoTypeVO();
            weatherInfoTypeVO.setStatus(value);
            weatherInfoTypeVO.setStatusDesc(WeatherInfoTypeEnum.getEnum(value).getDesc());
            weatherInfoTypeVOList.add(weatherInfoTypeVO);
        }
        return weatherInfoTypeVOList;
    }

}
